package com.spring.boot.controller;

import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.spring.boot.entity.MybatisUser;
import com.spring.boot.entity.Page;
import com.spring.boot.service.impl.UserServiceImpl;

@Controller
public class UserController {
	MybatisUser user = new MybatisUser();
	
	@Resource
	UserServiceImpl userService;
	
	@RequestMapping("/save")
	@ResponseBody
	public String save() {
		user.setName("123456");
		user.setPassword("123456");
		Integer num = null;
		try {
			num = userService.save(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return num.toString();
	}
	
	@RequestMapping("/query")
	@ResponseBody
	public MybatisUser query() throws Exception {
		user.setName("测试2");
		return (MybatisUser) userService.queryOfObject(user);
	}

	@RequestMapping("/updata")
	@ResponseBody
	public String updata() throws Exception {
		user.setName("测试2");
		MybatisUser mu = (MybatisUser) userService.queryOfObject(user);
		if(null == mu) {
			return "NO　Object";
		}else {
			mu.setName("测试3");
			Integer num = userService.updateOfName(mu);
			return num.toString();
		}
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public String delete() throws Exception{
		user.setName("测试3");
		MybatisUser mu = (MybatisUser) userService.queryOfObject(user);
		
		if(null == mu) {
			return "NO Object";
		}else {
			Integer num = userService.delete(mu);
			return num.toString();
		}
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/all")
	@ResponseBody
	public List<MybatisUser> queryAll() throws Exception{
		Page page = new Page();
		page.setCurrentPage(0);
		page.setShowCount(2);
		System.out.println(page.getStartSize() + " " + page.getEndSize());
		List<MybatisUser> ulist =  (List<MybatisUser>) userService.queryAll(page);
		return ulist;
	}
}
